package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
//@LogAnno
public class EmployeeController {


    @Autowired
    private IEmployeeService employeeService;

    /**
     * 添加/修改
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        if (null == employee.getId()) {
            // 添加
            employeeService.add(employee);
        } else {
            // 修改
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门数据")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
        return employeeService.findById(id);
    }

    /**
     * 分页查询
     */
    @PostMapping
//    @LogAnno(method = "queryByPage",methodDes = "分页查询部门")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryByPage(query);
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 根据店铺Id查询所有店铺员工
     * @param request
     * @return
     */
    @GetMapping("/selectByShopId")
    public List<Employee> selectByShopId(HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        return employeeService.selectByShopId(employee.getShopId());
    }
}
